package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;
import io.lkt.modules.sys.dao.BUserInfoDao;
import io.lkt.modules.sys.dao.WantedUsersDao;
import io.lkt.modules.sys.entity.BUserInfoEntity;
import io.lkt.modules.sys.entity.WantedUsersEntity;
import io.lkt.modules.sys.entity.WantedUsersLogEntity;
import io.lkt.modules.sys.service.WantedUsersService;
import io.lkt.modules.sys.wantedutils.ImageHelper;
import io.lkt.modules.sys.wantedutils.SimilarImageSearch;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;


@Service("wantedUsersService")
public class WantedUsersServiceImpl extends ServiceImpl<WantedUsersDao, WantedUsersEntity> implements WantedUsersService {
    @Autowired
    private WantedUsersDao wantedUsersDao;

    @Autowired
    BUserInfoDao bUserInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WantedUsersEntity> page = this.page(
                new Query<WantedUsersEntity>().getPage(params),
                new QueryWrapper<WantedUsersEntity>()
                        .orderByDesc("id")
        );

        return new PageUtils(page);
    }

    @Override
    public List<WantedUsersEntity> queryList(Map<String, Object> map) {
        return this.wantedUsersDao.queryList(map);
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {
        return this.wantedUsersDao.queryTotal(map);
    }

    @Override
    public WantedUsersEntity queryById(Integer id) {
        return this.wantedUsersDao.queryById(id);
    }

    @Override
    public int creatWanted(WantedUsersEntity wantedUsersEntity) {
        return this.wantedUsersDao.creatWanted(wantedUsersEntity);
    }

    @Override
    public void updateWanted(WantedUsersEntity wantedUsersEntity) {
        this.wantedUsersDao.updateWanted(wantedUsersEntity);
    }

    @Override
    public void deleteWantedByIds(List<Integer> list) {
        for (Integer i : list) {
            this.wantedUsersDao.deleteWantedByIds(i);
        }
    }

    @Override
    public boolean queryInWanted(BUserInfoEntity targetUser) {
        try {
//            Map<String, Object> map = new HashMap<>();
//            map.put("nick_name", targetUser.getNickName());

            List<WantedUsersEntity> wantedUsers = wantedUsersDao.queryByNickName(targetUser.getNickName());

            if (CollectionUtils.isNotEmpty(wantedUsers)) {

                String tarAvatarName = UUID.randomUUID().toString() + ".jpg";

                ImageHelper.downloadAvatarPic(targetUser.getAvatarUrl(), targetUser.getUserId(), tarAvatarName, ImageHelper.WX_AVATAR_DOWNLOAD_PATH);

                String tarAvatarPath = ImageHelper.WX_AVATAR_DOWNLOAD_PATH + targetUser.getUserId() + "/" + tarAvatarName;
                String tarHashCode = SimilarImageSearch.produceFingerPrint(tarAvatarPath);

                for (WantedUsersEntity wu : wantedUsers) {
                    String avatarName = UUID.randomUUID().toString() + ".jpg";
                    ImageHelper.downloadAvatarPic(wu.getAvatarUrl(), wu.getUserId(), avatarName, ImageHelper.WX_AVATAR_DOWNLOAD_PATH);
                    String avatarPath = ImageHelper.WX_AVATAR_DOWNLOAD_PATH + wu.getUserId() + "/" + avatarName;
                    String hashCode = SimilarImageSearch.produceFingerPrint(avatarPath);

                    int difference = SimilarImageSearch.hammingDistance(tarHashCode, hashCode);
                    FileUtils.forceDeleteOnExit(new File(avatarPath));
                    if (difference == 0) {
//                        WantedUsersEntity wantedUsersLog = new WantedUsersEntity();
//                        wantedUsersLog.setUserId(targetUser.getUserId());
//                        wantedUsersLog.setNickName(targetUser.getNickName());
//                        wantedUsersLog.setAvatarUrl(targetUser.getAvatarUrl());
//                        wantedUsersLog.setGzhCode(targetUser.getGzhCode());
//                        wantedUsersLog.setCreateBy("123456");
//                        wantedUsersLog.setCreateTime(new Date());
//
//                        wantedUsersDao.creatWanted(wantedUsersLog);
                        FileUtils.forceDeleteOnExit(new File(tarAvatarPath));
                        return true;
                    }
                }
                FileUtils.forceDeleteOnExit(new File(tarAvatarPath));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int addWantedUsersLog(WantedUsersLogEntity wantedUsersLog) {
        return this.wantedUsersDao.addWantedUsersLog(wantedUsersLog);
    }
}
